package ggze.GGZEServer.home.service.impl;

import ggze.GGZEServer.home.dao.inter.*;
import ggze.GGZEServer.home.entity.*;
import ggze.GGZEServer.home.enums.ShopEnum;
import ggze.GGZEServer.home.enums.StatusEnum;
import ggze.GGZEServer.home.enums.TypeEnum;
import ggze.GGZEServer.home.service.BlanceService;
import ggze.GGZEServer.home.service.ExpendService;
import ggze.GGZEServer.home.service.InIndentService;
import ggze.actuator.ActuatorController;
import ggze.esp.dao.inter.FspUserDao;
import ggze.esp.entity.FspUser;
import ggze.tool.ReturnBody;
import ggze.tool.TimeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class InIndentServiceImpl implements InIndentService {
    @Autowired
    private FspUserDao fspUserDao;
    @Autowired
    private SoShopDao soShopDao;
    @Autowired
    private SoTeamUserDao soTeamUserDao;
    @Autowired
    private SoIndentDao soIndentDao;
    @Autowired
    private SoBlanceDao soBlanceDao;
    @Autowired
    private ActuatorController actuatorController;
    @Autowired
    private SoExpendRecordDao soExpendRecordDao;
    @Autowired
    private BlanceService blanceService;
    @Autowired
    private ExpendService expendService;
    @Transactional
    @Override
    public ReturnBody addInIndent(SoIndent soIndent, String indentType) {
        ReturnBody returnBody = new ReturnBody();
        try {
            if (fspUserDao.isSelect("uuid", soIndent.getF_BY_USER_ID()) == null) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                returnBody.setCode("1");
                returnBody.setTitle("用户不存在");
                return returnBody;
            }
            if (soShopDao.isSelect("uuid", soIndent.getF_SHOP_ID()) == null) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                returnBody.setCode("1");
                returnBody.setTitle("商品不存在");
                return returnBody;
            }
            SoShop sp = soShopDao.isSelect("uuid", soIndent.getF_SHOP_ID());
            soIndent.setF_SHOP_NAME(sp.getF_SHOP_NAME());
            soIndent.setF_SHOP_TYPE(String.valueOf(sp.getF_SHOP_TYPE()));
            soIndent.setF_SELES(ShopEnum.getLables(String.valueOf(sp.getF_SHOP_TYPE())));
            soIndent.setF_CONVERT_JF(sp.getF_CONVERT_JF()* soIndent.getF_SHOP_NUM());
            soIndent.setF_CONSUME_JF(sp.getF_CONSUME_JF()* soIndent.getF_SHOP_NUM());
            if (soIndent.getF_SHOP_NUM() == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                returnBody.setCode("1");
                returnBody.setTitle("请选择最少一个商品！");
                return returnBody;
            }
            soIndent.setF_PRICE(sp.getF_PRICE());

            if (sp.getF_STOCK_NUM() <= 0 || soIndent.getF_SHOP_NUM() - sp.getF_STOCK_NUM() > 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                returnBody.setCode("1");
                returnBody.setTitle("库存不足！");
                return returnBody;
            }
            //soIndent.setF_SHOP_TYPE("0");
            soIndent.setF_MONEY(soIndent.getF_PRICE() * soIndent.getF_SHOP_NUM());
            //soIndent.setF_STATUS("0");
            //soIndent.setF_MAIL_STATUS("0");

            //分享人
            FspUser usf = soTeamUserDao.seachFirst(soIndent.getF_BY_USER_ID());
            if (usf != null&&!usf.getUuid().equals(soIndent.getF_BY_USER_ID())) {
                soIndent.setF_IS_FX(1);
                soIndent.setF_FX_FIRST(usf.getUuid());
            }
            usf = soTeamUserDao.seachSecond(soIndent.getF_BY_USER_ID());
            if (usf != null&&!usf.getUuid().equals(soIndent.getF_BY_USER_ID())) {
                soIndent.setF_FX_SECOND(usf.getUuid());
            }
            String time = TimeUtils.getTimeFormat();
            soIndent.setF_CREATE_TIME(time);
            soIndent.setF_METHOD_TIME(time);
            soIndent.setF_INDENT_TYPE(indentType);
            soIndent.setF_SELES1(TypeEnum.INDENT.getLables(indentType));

            soIndent.setF_STATUS(0);
            soIndent.setF_SELES2(StatusEnum.INDENT_STATUS.getLables(String.valueOf(0)));


            soIndent.setF_AFFIRM_STATUS(0);
            soIndent.setF_SELES4(StatusEnum.INDENT_AFFIRM_STATUS.getLables("0"));

            soIndent.setF_QUEUE_STATUS("0");
            soIndent.setF_SELES5(StatusEnum.INDENT_QUEUE_STATUS.getLables("0"));
            soIndent.setUuid("");

            //信用区生成邮寄打包
            if (soIndent.getF_SHOP_TYPE().equals("0")||soIndent.getF_SHOP_TYPE().equals("2")) {
                soIndent.setF_MAIL_STATUS("1");
                soIndent.setF_SELES3(StatusEnum.INDENT_MAIL_STATUS.getLables("1"));
            }else{
                soIndent.setF_MAIL_STATUS("0");
                soIndent.setF_SELES3(StatusEnum.INDENT_MAIL_STATUS.getLables("0"));
            }
            FspUser user = fspUserDao.isSelect("uuid",soIndent.getF_BY_USER_ID());
            //查询积分是否充足
            SoBlance soBlance = soBlanceDao.isSelect("f_user_id", soIndent.getF_BY_USER_ID());
            if(soBlance==null){
                blanceService.createBlance(user);
            }
            soBlance = soBlanceDao.isSelect("f_user_id", soIndent.getF_BY_USER_ID());
            //活动区查询积分
            if (soIndent.getF_SHOP_TYPE().equals("1")) {
                if (soBlance.getF_CONSUME_JF() - soIndent.getF_CONSUME_JF() < 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    returnBody.setCode("1");
                    returnBody.setTitle("积分不足！");
                    return returnBody;
                }
            }
            //兑换区查询积分
            if (soIndent.getF_SHOP_TYPE().equals("2")) {
                if (soBlance.getF_CONVERT_JF() - soIndent.getF_CONVERT_JF() < 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    returnBody.setCode("1");
                    returnBody.setTitle("积分不足！");
                    return returnBody;
                }
                soIndent.setF_MAIL_STATUS("1");
                soIndent.setF_SELES3(StatusEnum.INDENT_MAIL_STATUS.getLables("1"));
            }
            String uuid = (String) actuatorController.execute(soIndent, "add", soIndentDao);

            //产生支出



            if (soIndent.getF_SHOP_TYPE().equals("0")) {
                soBlance.setF_EXPEND_JE(soIndent.getF_MONEY());
                expendService.changeExpend(soBlance,"0");
                /*soExpendRecord.setF_NOTE("信用区购买");
                //不加入匹配
                soExpendRecord.setF_SELES2("1");
                //付款状态
                soExpendRecord.setF_SELES3("0");
                //确认状态
                soExpendRecord.setF_SELES4("0");

                soExpendRecord.setF_SELES1("XY"+uuid);
                actuatorController.execute(soExpendRecord,"add",soExpendRecordDao);*/
            }
            if (soIndent.getF_SHOP_TYPE().equals("1")) {
                /*soExpendRecord.setF_NOTE("活动区购买");
                soExpendRecord.setF_SELES2("0");
                //付款状态
                soExpendRecord.setF_SELES3("0");
                //确认状态
                soExpendRecord.setF_SELES4("0");

                soExpendRecord.setF_SELES1("HD"+uuid);
                actuatorController.execute(soExpendRecord,"add",soExpendRecordDao);*/
                soBlance.setF_EXPEND_JE(soIndent.getF_MONEY());
                expendService.changeExpend(soBlance,"1");
            }
            //销量
            sp.setF_STOCK_NUM(sp.getF_STOCK_NUM() - soIndent.getF_SHOP_NUM());
            sp.setF_SELES1(String.valueOf(Integer.parseInt(sp.getF_SELES1()) - soIndent.getF_SHOP_NUM()));
            sp.setF_METHOD_TIME(TimeUtils.getTimeFormat());

            actuatorController.execute(sp, "update", soShopDao);
            returnBody.setCode("0");
            returnBody.setTitle("下单成功！");
            return returnBody;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            returnBody.setCode("1");
            returnBody.setTitle("系统错误！");
            return returnBody;
        }
    }

    @Override
    public ReturnBody deleteInIndent(SoIndent soIndent) {
        return null;
    }

    @Override
    public ReturnBody mailInIndent(SoIndent soIndent) {
        return null;
    }
}
